/*===============================
 *Title      :  EncryptFileActivity.java
 *Created  :[ 2012-3-23 下午3:10:51 ] by fanlt
 * ==============================
 * Description:   
 * Copyright  :  Copyright (c) 2012
 * Company    :  Flyfot
 * @author  <a href="fanlt@flyfot.cn">fanlt</a>
 *
 */
package org.zxstudio.encrypt;

import java.util.List;

import org.zxstudio.encrypt.provider.EncryptProvider;
import org.zxstudio.encrypt.task.EncryptListener;
import org.zxstudio.encrypt.task.TaskController;
import org.zxstudio.encrypt.utils.Constant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

/**
 * @author fan
 * 
 */
public class EncryptFileActivity extends Activity implements OnClickListener,
		OnItemClickListener, EncryptListener.CallBack {
	private static final String TAG = "EncryptFileActivity";
	private static final boolean DEBUG = true;

	private Cursor mCursor;
	private Button mEncryptBtn;
	private Button mCancelBtn;
	private GridView mGridView;
	private EncryptFileAdapter mAdapter = null;
	private ContentResolver mCr = null;
	QueryEncryptTask mAsyncTask = null;
	TaskController mTc = null;
	ProgressDialog mProgressDialog = null;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mAdapter.notifyDataSetChanged();
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.encrypt_file_view);
		initRes();
	}

	/**
	 * 
	 */
	private void initRes() {
		mCr = this.getContentResolver();
		mTc = TaskController.getInstance(this);
		mTc.registerEncryptListener(this);
		mEncryptBtn = (Button) findViewById(R.id.decrypt);
		mEncryptBtn.setOnClickListener(this);
		mCancelBtn = (Button) findViewById(R.id.cancel);
		mCancelBtn.setOnClickListener(this);
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setOnItemClickListener(this);
		mAdapter = new EncryptFileAdapter(this, mCursor);
		mAdapter.setMode(EncryptFileAdapter.SELECT_MODE);
		mGridView.setAdapter(mAdapter);
		mAsyncTask = new QueryEncryptTask();
		mAsyncTask.execute(0);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
	 * .AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position,
			long id) {
		mAdapter.updateSelectItem(view, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.decrypt:
			List<Long> items = mAdapter.getSelectedItems();
			if (items.size() > 0) {
				mTc.decrypt(items);
				mProgressDialog = ProgressDialog.show(this, "", getResources()
						.getString(R.string.decrypt_file));
			}
			
			break;
		case R.id.cancel:
			mAdapter.selectAllItems();
			break;
		}
	}

	private class QueryEncryptTask extends AsyncTask<Integer, Integer, Void> {

		private ProgressDialog dialog;

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			mAdapter.changeCursor(mCursor);
			mAdapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = ProgressDialog.show(
					EncryptFileActivity.this,
					"",
					EncryptFileActivity.this.getResources().getString(
							R.string.explorer_sd_file));
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#doInBackground(Params[])
		 */
		@Override
		protected Void doInBackground(Integer... params) {
			mCursor = mCr.query(EncryptProvider.ENCRYPT_URI,
					EncryptProvider.sPROJECTIONS, "file_type = "
							+ Constant.TYPE_FILE, null, null);
			return null;
		}
	}

	public void notifyResult(List<String> sucessedFiles,
			List<String> failedFiles) {
		mHandler.sendEmptyMessage(0);
	}
}
